<?php
if (!class_exists('site_privilege_model')) {
    require_once 'site_privilege_model.php';
}

class site_role_model extends CI_Model {

    protected $_instance = array(
        'table' => 'site_role',
        'primaryKey' => 'id',
    );
    public $_data = null;

    public function get_roles() {
        $query = $this->db->get($this->_instance['table']);
        $list_role = array();
        foreach ($query->result() as $row) {
            $list_role[] = $row;
        }
        $query->free_result();
        return $list_role;
    }

    public function add_role($data) {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        if (empty($data['name'])) {
            throw new Exception('Tên chức vụ không được để trống');
            return false;
        }
        $data['user_id'] = $CI->session->userdata('admin_id');
        if (empty($data['error'])) {
            $this->db->insert($this->_instance['table'], $data);
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }
    
    public function update_role($id, $data) {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        if (empty($data['name'])) {
            throw new Exception('Tên chức vụ không được để trống');
            return false;
        }
        $data['user_id'] = $CI->session->userdata('admin_id');
        if (empty($data['error'])) {
            $this->db->where('id', $id);
            $this->db->update($this->_instance['table'], $data);
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }
    
    public function load_by_id($id) {
        $query = $this->db->get_where($this->_instance['table'], array('id' => $id), 1, 0);
        $this->_data = $query->row();
        if (empty($this->_data->privileges)) {
            $this->_data->privileges = array();
        } else {
            $this->_data->privileges = unserialize($this->_data->privileges);
        }
        if (empty($this->_data->system_privileges)) {
            $this->_data->system_privileges = array();
        } else {
            $this->_data->system_privileges = unserialize($this->_data->system_privileges);
        }
        $query->free_result();
        return $this->_data;
    }
    
    public function delete_role($id) {
        $this->db->delete($this->_instance['table'], array('id' => $id)); 
        return $this;
    }

    private function auto_render($data) {
        if (empty($data['privileges'])) {
            $data['privileges'] = array();
        }
        $data['privileges'] = serialize($data['privileges']);
        if (empty($data['system_privileges'])) {
            $data['system_privileges'] = array();
        }
        $data['system_privileges'] = serialize($data['system_privileges']);
        return $data;
    }

    public function get_privileges($id) {
        $query = $this->db->query('SELECT privileges, system_privileges FROM site_role WHERE id = ' . $id . ' LIMIT 0, 1');
        $result = $query->row();
        $privilege_ids = implode(',', @unserialize($result->privileges));
        $list_privilege = array();
        $query->free_result();
        if (!empty($privilege_ids)) {
            $query = $this->db->query('SELECT * FROM site_privilege WHERE id IN ('.$privilege_ids.')');
            foreach ($query->result() as $row) {
                $list_privilege[$row->url] = $row;
            }
        }
        $privilege_model = new site_privilege_model();
        $all_system_privileges = $privilege_model->get_system_privilege_list();
        $system_privileges = @unserialize($result->system_privileges);
        if (is_array($system_privileges) && count($system_privileges) > 0) {
            foreach ($system_privileges as $privilege_alias) {
                $item = $all_system_privileges[$privilege_alias];
                $list_privilege[$item['url']] = $item;
            }
        }
        return $list_privilege;
    }
}

?>